//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Index.h"
using namespace Cephei::QL;
#include <gen/QL/Times/Calendar.h>
using namespace Cephei::QL::Times;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::CIndex::CIndex (boost::shared_ptr<QuantLib::Index>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIndex = NULL;
#endif
	_ppIndex = &childNative;
    
}
Cephei::QL::CIndex::CIndex (QuantLib::Index& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phIndex = NULL;
#endif
	_ppIndex = new boost::shared_ptr<QuantLib::Index> (&childNative);
    
    _IndexOwner = owner;
    
}

Cephei::QL::CIndex::CIndex (CIndex^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIndex = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIndex = new boost::shared_ptr<QuantLib::Index> (copy->GetShared());
        
    }
}
Cephei::QL::CIndex::CIndex (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIndex = NULL;
#endif
	if (!t->IsSubclassOf(CIndex::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CIndex::CIndex (QuantLib::Handle<QuantLib::Index>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIndex = &childNative;
	_ppIndex = &static_cast<boost::shared_ptr<QuantLib::Index>>(childNative.currentLink());
    
    _IndexOwner = owner;
}
Cephei::QL::CIndex::CIndex (QuantLib::Handle<QuantLib::Index> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIndex = &childNative;
	_ppIndex = &static_cast<boost::shared_ptr<QuantLib::Index>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CIndex::CIndex (QuantLib::Index childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIndex = NULL;
#endif
	_ppIndex = new boost::shared_ptr<QuantLib::Index> (new QuantLib::Index (childNative));
    
}
#endif

Cephei::QL::CIndex::~CIndex ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppIndex != NULL)
    {
	    delete _ppIndex;
        _ppIndex = NULL;
    }
}
Cephei::QL::CIndex::!CIndex ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppIndex != NULL)
    {
	    delete _ppIndex;
    }
}
QuantLib::Index& Cephei::QL::CIndex::GetReference ()
{
    if (_ppIndex == NULL) throw gcnew NativeNullException ();
	return **_ppIndex;
}
boost::shared_ptr<QuantLib::Index>& Cephei::QL::CIndex::GetShared ()
{
    if (_ppIndex == NULL) throw gcnew NativeNullException ();
	return *_ppIndex;
}
QuantLib::Index* Cephei::QL::CIndex::GetPointer ()
{
    if (_ppIndex == NULL) throw gcnew NativeNullException ();
	return &**_ppIndex;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Index>& Cephei::QL::CIndex::GetHandle ()
{
	if (_phIndex == NULL)
	{
		_phIndex = new Handle<QuantLib::Index> (*_ppIndex);
	}
	return *_phIndex;
}
#endif
bool Cephei::QL::CIndex::HasNative () 
{
	return (_ppIndex != NULL);
}

Cephei::QL::IIndex^ Cephei::QL::CIndex::AddFixing (DateTime fixingDate, Double fixing, Microsoft::FSharp::Core::FSharpOption<Boolean>^ forceOverwrite)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate);
        QuantLib::Real _fixing = (QuantLib::Real)ValueHelper::Convert (fixing);
        bool _forceOverwrite = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (forceOverwrite) ? (bool)ValueHelper::Convert (forceOverwrite->Value) : false); //9a
    	(*_ppIndex)->addFixing ( _fixingDate,  _fixing,  _forceOverwrite );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IIndex^ Cephei::QL::CIndex::ClearFixings::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppIndex)->clearFixings ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CIndex::Fixing (DateTime fixingDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ forecastTodaysFixing)
{
    try
    {
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate);
        bool _forecastTodaysFixing = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (forecastTodaysFixing) ? (bool)ValueHelper::Convert (forecastTodaysFixing->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIndex)->fixing ( _fixingDate,  _forecastTodaysFixing );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::ICalendar^ Cephei::QL::CIndex::FixingCalendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppIndex)->fixingCalendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = gcnew Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::CIndex::IsValidFixingDate (DateTime fixingDate)
{
    try
    {
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate);
    	bool _rv = (bool)(*_ppIndex)->isValidFixingDate ( _fixingDate );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::CIndex::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppIndex)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

